import Vue from 'vue'
import Router from 'vue-router'

// import Login from './components/Login.vue'
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ './components/Login.vue')
// import Home from './components/Home.vue'
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ './components/Home.vue')
// import Welcome from './components/Welcome.vue'
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ './components/Welcome.vue')

// ======= 原料进销存 =======
// 原料库存情况
const MaterialList = () => import(/* webpackChunkName: "Material" */ './components/material/house/list.vue')
const MaterialSave = () => import(/* webpackChunkName: "Material" */ './components/material/add.vue')
// 原料入库明细
const MaterialIn = () => import(/* webpackChunkName: "Material" */ './components/material/in-list.vue')
// 原料采购单
const MaterialPurchase = () => import(/* webpackChunkName: "Material" */ './components/material/purchase/list.vue')
const MaterialMulti = () => import(/* webpackChunkName: "Material" */ './components/material/purchase/multi.vue')
const MaterialDetail = () => import(/* webpackChunkName: "Material" */ './components/material/purchase/detail.vue')
const MaterialUpdate = () => import(/* webpackChunkName: "Material" */ './components/material/purchase/update.vue')
const MaterialCheck = () => import(/* webpackChunkName: "Material" */ './components/material/purchase/check.vue')
// 原料出仓订单列表
const MaterialOrder = () => import(/* webpackChunkName: "Material" */ './components/material/order.vue')
// 原料仓出仓明细
const MaterialSell = () => import(/* webpackChunkName: "Material" */ './components/material/house/sell.vue')
// const MaterialOut = () => import(/* webpackChunkName: "Material" */ './components/material/out-list.vue')

// 肉码模块
const ProductList = () => import(/* webpackChunkName: "Product" */ './components/product/list.vue')
const ProductSave = () => import(/* webpackChunkName: "Product" */ './components/product/add.vue')
const ProductOrder = () => import(/* webpackChunkName: "Product" */ './components/product/order.vue')
const ProductSell = () => import(/* webpackChunkName: "Product" */ './components/product/sell.vue')
const ProductIn = () => import(/* webpackChunkName: "Product" */ './components/product/in-list.vue')
// const ProductOut = () => import(/* webpackChunkName: "Product" */ './components/product/out-list.vue')

// 商品模块
const GoodsList = () => import(/* webpackChunkName: "Goods" */ './components/goods/list.vue')
const GoodsSave = () => import(/* webpackChunkName: "Goods" */ './components/goods/add.vue')
const GoodsUpdate = () => import(/* webpackChunkName: "Goods" */ './components/goods/update.vue')

// 申请审批
const Print = () => import(/* webpackChunkName: "Print" */ './components/apply/applied/print.vue')
const ApplyList = () => import(/* webpackChunkName: "Apply" */ './components/apply/apply/list.vue')
const ApplyCheck = () => import(/* webpackChunkName: "Apply" */ './components/apply/apply/check.vue')
const ApplyDetail = () => import(/* webpackChunkName: "Apply" */ './components/apply/apply/detail.vue')
const ApplyUpdate = () => import(/* webpackChunkName: "Apply" */ './components/apply/apply/update.vue')
const AppliedList = () => import(/* webpackChunkName: "Apply" */ './components/apply/applied/list.vue')
const AppliedDetail = () => import(/* webpackChunkName: "Apply" */ './components/apply/applied/detail.vue')

// 门店仓库
const StoreList = () => import(/* webpackChunkName: "Store" */ './components/store-house/store-list.vue')

// 仓库退货单
const StoreBackList = () => import(/* webpackChunkName: "Store" */ './components/store-back/list.vue')
const StoreBackDetail = () => import(/* webpackChunkName: "Store" */ './components/store-back/detail.vue')
const StoreBackSave = () => import(/* webpackChunkName: "Store" */ './components/store-back/add.vue')

// 外部采购
const OuterList = () => import(/* webpackChunkName: "Store" */ './components/store-house/outer-purchase/list.vue')
const OuterSave = () => import(/* webpackChunkName: "Store" */ './components/store-house/outer-purchase/add.vue')
const OuterDetail = () => import(/* webpackChunkName: "Store" */ './components/store-house/outer-purchase/detail.vue')

// 内部采购
const InnerList = () => import(/* webpackChunkName: "Store" */ './components/store-house/inner-purchase/list.vue')
const InnerSave = () => import(/* webpackChunkName: "Store" */ './components/store-house/inner-purchase/add.vue')
const InnerDetail = () => import(/* webpackChunkName: "Store" */ './components/store-house/inner-purchase/detail.vue')
const InnerUpdate = () => import(/* webpackChunkName: "Store" */ './components/store-house/inner-purchase/update.vue')

// 门店损耗
const StoreLossList = () => import(/* webpackChunkName: "Store" */ './components/store-house/store-loss/list.vue')
const StoreLossSave = () => import(/* webpackChunkName: "Store" */ './components/store-house/store-loss/add.vue')
const StoreLossDetail = () => import(/* webpackChunkName: "Store" */ './components/store-house/store-loss/detail.vue')
const StoreLossUpdate = () => import(/* webpackChunkName: "Store" */ './components/store-house/store-loss/update.vue')

// 物资调拨
const AllocationList = () => import(/* webpackChunkName: "Allocation" */ './components/allocation/list.vue')
const AllocationDetail = () => import(/* webpackChunkName: "Allocation" */ './components/allocation/detail.vue')
const AllocationSave = () => import(/* webpackChunkName: "Allocation" */ './components/allocation/save.vue')
const AllocationUpdate = () => import(/* webpackChunkName: "Allocation" */ './components/allocation/update.vue')
const AllocationCheck = () => import(/* webpackChunkName: "Allocation" */ './components/allocation/check.vue')

// 肉码成本
const ProcessList = () => import(/* webpackChunkName: "Process" */ './components/process/list.vue')
const ProcessDetail = () => import(/* webpackChunkName: "Process" */ './components/process/detail.vue')
const ProcessUpdate = () => import(/* webpackChunkName: "Process" */ './components/process/update.vue')

// 出入库明细
const InoutList = () => import(/* webpackChunkName: "Inout" */ './components/inout/list.vue')

// 高级设置
const ProcessBack = () => import(/* webpackChunkName: "Setting" */ './components/setting/process-back.vue')
const OprateStore = () => import(/* webpackChunkName: "Setting" */ './components/setting/operate-store.vue')

// 下载表格
const ExportInout = () => import(/* webpackChunkName: "Export" */ './components/export/download.vue')

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', name: 'welcome', component: Welcome },
        { path: '/goods-list', name: 'goods-list', component: GoodsList },
        { path: '/goods-save', name: 'goods-save', component: GoodsSave },
        { path: '/goods-update', name: 'goods-update', component: GoodsUpdate },
        { path: '/material-list', name: 'material-list', component: MaterialList },
        { path: '/material-save', name: 'material-save', component: MaterialSave },
        { path: '/material-multi', name: 'material-multi', component: MaterialMulti },
        { path: '/material-order', name: 'material-order', component: MaterialOrder },
        { path: '/material-sell', name: 'material-sell', component: MaterialSell },
        { path: '/material-in', name: 'material-in', component: MaterialIn },
        // { path: '/material-out', name: 'material-out', component: MaterialOut },
        { path: '/material-purchase', name: 'material-purchase', component: MaterialPurchase },
        { path: '/material-purchase-detail', name: 'material-purchase-detail', component: MaterialDetail },
        { path: '/material-purchase-update', name: 'material-purchase-update', component: MaterialUpdate },
        { path: '/material-purchase-check', name: 'material-purchase-check', component: MaterialCheck },
        { path: '/product-list', name: 'product-list', component: ProductList },
        { path: '/product-save', name: 'product-save', component: ProductSave },
        { path: '/product-order', name: 'product-order', component: ProductOrder },
        { path: '/product-sell', name: 'product-sell', component: ProductSell },
        { path: '/product-in', name: 'product-in', component: ProductIn },
        // { path: '/product-out', name: 'product-out', component: ProductOut },
        { path: '/process-list', name: 'process-list', component: ProcessList },
        { path: '/process-detail', name: 'process-detail', component: ProcessDetail },
        { path: '/process-update', name: 'process-update', component: ProcessUpdate },
        { path: '/inout-list', name: 'inout-list', component: InoutList },
        { 
          path: '/apply-list', 
          name: 'apply-list', 
          component: ApplyList,
          children: [
            { path: '/apply-check', name: 'apply-check', component: ApplyCheck },
            { path: '/apply-detail', name: 'apply-detail', component: ApplyDetail },
            { path: '/apply-update', name: 'apply-update', component: ApplyUpdate }
          ]
        },
        { 
          path: '/applied-list', 
          name: 'applied-list', 
          component: AppliedList ,
          children: [
            { path: '/applied-detail', name: 'applied-detail', component: AppliedDetail }
          ]
        },
        { path: '/store-house-list', name: 'store-house-list', component: StoreList },
        { 
          path: '/store-back-list', 
          name: 'store-back-list', 
          component: StoreBackList,
          children: [
            { path: '/store-back-detail', name: 'store-back-detail', component: StoreBackDetail },
            { path: '/store-back-save', name: 'store-back-save', component: StoreBackSave }
          ]
        },
        { path: '/outer-purchase-list', name: 'outer-purchase-list', component: OuterList },
        { path: '/outer-purchase-save', name: 'outer-purchase-save', component: OuterSave },
        { path: '/outer-purchase-detail', name: 'outer-purchase-detail', component: OuterDetail },
        { path: '/inner-purchase-list', name: 'inner-purchase-list', component: InnerList },
        { path: '/inner-purchase-save', name: 'inner-purchase-save', component: InnerSave },
        { path: '/inner-purchase-detail', name: 'inner-purchase-detail', component: InnerDetail },
        { path: '/inner-purchase-update', name: 'inner-purchase-update', component: InnerUpdate },
        { path: '/store-loss-list', name: 'store-loss-list', component: StoreLossList },
        { path: '/store-loss-save', name: 'store-loss-save', component: StoreLossSave },
        { path: '/store-loss-detail', name: 'store-loss-detail', component: StoreLossDetail },
        { path: '/store-loss-update', name: 'store-loss-update', component: StoreLossUpdate },
        { 
          path: '/material-allocation-list', 
          name: 'material-allocation-list', 
          component: AllocationList,
          children: [
            { path: '/material-allocation-detail', name: 'material-allocation-detail', component: AllocationDetail },
            { path: '/material-allocation-save', name: 'material-allocation-save', component: AllocationSave },
            { path: '/material-allocation-update', name: 'material-allocation-update', component: AllocationUpdate },
            { path: '/material-allocation-check', name: 'material-allocation-check', component: AllocationCheck }
          ]
        },
        { path: '/export', name: 'export', component: ExportInout },
        { path: '/process-back', name: 'process-back', component: ProcessBack },
        { path: '/operate-store', name: 'operate-store', component: OprateStore }
      ]
    },
    { path: '/print', name: 'print', component: Print }
  ]
})
// 设置登录过期时间（一天）
let EXPIRESTIME = 86400000

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转
  if (to.name !== 'print') {
    document.querySelector('html').setAttribute('style', 'height: 100%;margin: 0;padding: 0;min-width: 1366px;')
    document.querySelector('body').setAttribute('style', 'height: 100%;margin: 0;padding: 0;min-width: 1366px;')
  } else {
    document.querySelector('html').setAttribute('style', 'height: 0;padding: 0;min-width: 920px;')
    document.querySelector('body').setAttribute('style', 'height: 0;padding: 0;min-width: 920px;')
  }
  const str = to.name
  if (str && str.indexOf('list') !== -1) {
    localStorage.removeItem('goodsId')
    localStorage.removeItem('materialAddId')
    localStorage.removeItem('materialId')
    localStorage.removeItem('applyId')
    localStorage.removeItem('appliedId')
    localStorage.removeItem('productId')
    localStorage.removeItem('innerId')
    localStorage.removeItem('outerId')
    localStorage.removeItem('lossId')
    localStorage.removeItem('ProcessTime')
    localStorage.removeItem('houseInId')
  }
  const tokenStr = localStorage.getItem('token')
  if (!localStorage.getItem('token') || !localStorage.getItem('gj_access')) {
    if (to.path === '/login') { // 未登录且已经在Login页了，直接放行
      return next()
    }
    return next('/login')
  }
  if (to.path === '/login') {
    return next('/home')
  }
  let date = new Date().getTime()
  // 如果大于就是过期了，如果小于或等于就还没过期
  if (date - localStorage.getItem('time') > EXPIRESTIME) {
    localStorage.removeItem('token')
    localStorage.removeItem('time')
    localStorage.removeItem('userid')
    localStorage.removeItem('access')
    localStorage.removeItem('gj_access')
    next({
      name: 'login'
    })
  }
  next()
})

export default router
